package com.ling.controller.admin;

import com.ling.bean.Area;
import com.ling.bean.Msg;
import com.ling.service.AreaService;
import com.ling.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class AdminAreaController {

    private final AreaService areaService;

    @Autowired
    public AdminAreaController(AreaService areaService) {
        this.areaService = areaService;
    }

    //页面跳转
    @RequestMapping(value = "/adminArea",method = RequestMethod.GET)
    public String goToAdminArea() {
        return "/admin/adminArea";
    }


    //跳转到添加领域页面
    @RequestMapping(value = "/adminAddArea",method = RequestMethod.GET)
    public String goToAdminAddArea() {
        return "/admin/adminAddArea";
    }


    //添加领域
    @RequestMapping(value = "/adminAddArea",method = RequestMethod.POST)
    @ResponseBody
    public Msg adminAddArea(Area area, @Autowired HttpSession session) {
        if (session.getAttribute("admin") == null) {
            //没有登录
            return Msg.fail().add("error","session失效，请重新登录");
        }
        area.setId(StringUtil.getRandomString(16));
        areaService.saveArea(area);
        return Msg.success();
    }

    //查询领域,不需要验证用户是否登录，所有用户均需要查询领域
    @RequestMapping(value = "/allArea",method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllAddArea() {
        return Msg.success().add("areas",areaService.getArea());
    }

    //管理员查看指定id的领域
    @RequestMapping(value = "/adminViewArea/{id}",method = RequestMethod.GET)
    public String goToAdminViewArea(@Autowired HttpSession session,@PathVariable String id) {
        if (session.getAttribute("admin") == null) {
            //没有登录,跳转到登陆页面
            return "redirect:/adminLogin";
        }else{
            //已经登录，跳转到查看页面，并保存该领域
            session.setAttribute("area", areaService.getArea(id));
            return "/admin/adminViewArea";
        }
    }

    //管理员修改领域
    @RequestMapping(value = "/adminUpdateArea",method = RequestMethod.POST)
    @ResponseBody
    public Msg adminUpdateArea(Area area,@Autowired HttpSession session) {
        //判断是否登录
        if (session.getAttribute("admin") == null) {
            //没有登录
            return Msg.fail().add("error","session失效，请重新登录");
        }
        areaService.updateArea(area);
        return Msg.success();
    }

    //管理员删除领域
    @RequestMapping(value = "/adminDelArea",method = RequestMethod.POST)
    @ResponseBody
    public Msg adminDelArea(@RequestParam String id, @Autowired HttpSession session) {
        //判断是否登录
        if (session.getAttribute("admin") == null) {
            //没有登录
            return Msg.fail().add("error","session失效，请重新登录");
        }else{
            //已经登陆,删除该领域
            areaService.delArea(id);
            return Msg.success();
        }
    }

}
